home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / xvisrc.zip / DEFSCR.C < prev    next >
C/C++ Source or Header  |  1992-07-28  |  6KB  |  327 lines

  1. /* Copyright (c) 1990,1991,1992 Chris and John Downey */
  2. #ifndef lint
  3. static char *sccsid = "@(#)defscr.c    2.4 (Chris & John Downey) 9/4/92";
  4. #endif
  5.  
  6. /***
  7.  
  8. * program name:
  9.     xvi
  10. * function:
  11.     PD version of UNIX "vi" editor, with extensions.
  12. * module name:
  13.     defscr.c
  14. * module function:
  15.     VirtScr interface using old style porting functions.
  16.     We assume newscr() is only called once; it is an
  17.     error for it to be called more than once.
  18. * history:
  19.     STEVIE - ST Editor for VI Enthusiasts, Version 3.10
  20.     Originally by Tim Thompson (twitch!tjt)
  21.     Extensive modifications by Tony Andrews (onecom!wldrdg!tony)
  22.     Heavily modified by Chris & John Downey
  23.  
  24. ***/
  25.  
  26. #include "xvi.h"
  27.  
  28. static    VirtScr    *newscr P((VirtScr *));
  29. static    void    closescr P((VirtScr *));
  30. static    int    getrows P((VirtScr *));
  31. static    int    getcols P((VirtScr *));
  32. static    void    clear_all P((VirtScr *));
  33. static    void    clear_line P((VirtScr *, int, int));
  34. static    void    xygoto P((VirtScr *, int, int));
  35. static    void    xyadvise P((VirtScr *, int, int, int, char *));
  36. static    void    put_char P((VirtScr *, int, int, int));
  37. static    void    put_str P((VirtScr *, int, int, char *));
  38. static    void    ins_str P((VirtScr *, int, int, char *));
  39. static    void    pset_colour P((VirtScr *, int));
  40. static    int    colour_cost P((VirtScr *));
  41. static    int    scroll P((VirtScr *, int, int, int));
  42. static    void    flushout P((VirtScr *));
  43. static    void    pbeep P((VirtScr *));
  44.  
  45. VirtScr    defscr = {
  46.     NULL,        /* pv_window        */
  47.     0,            /* pv_rows        */
  48.     0,            /* pv_cols        */
  49.  
  50.     newscr,        /* v_new        */
  51.     closescr,        /* v_close        */
  52.     getrows,        /* v_rows        */
  53.     getcols,        /* v_cols        */
  54.     clear_all,        /* v_clear_all        */
  55.     clear_line,        /* v_clear_line        */
  56.     xygoto,        /* v_goto        */
  57.     xyadvise,        /* v_advise        */
  58.     put_str,        /* v_write        */
  59.     put_char,        /* v_putc        */
  60.     pset_colour,    /* v_set_colour        */
  61.     colour_cost,    /* v_colour_cost    */
  62.     flushout,        /* v_flush        */
  63.     pbeep,        /* v_beep        */
  64.  
  65.     ins_str,        /* v_insert        */
  66.     scroll,        /* v_scroll        */
  67.     NULL,        /* v_flash        */
  68.     NULL,        /* v_status        */
  69.     NULL,        /* v_activate        */
  70. };
  71.  
  72. int
  73. main(argc, argv)
  74. int    argc;
  75. char    *argv[];
  76. {
  77.     xvEvent    event;
  78.     long    timeout = 0;
  79.  
  80.     /*
  81.      * Set up the system and terminal interfaces. This establishes
  82.      * the window size, changes to raw mode and does whatever else
  83.      * is needed for the system we're running on.
  84.      */
  85.     sys_init();
  86.  
  87.     if (!can_inschar) {
  88.     defscr.v_insert = NULL;
  89.     }
  90.     if (!can_scroll_area && !can_ins_line && !can_del_line) {
  91.     defscr.v_scroll = NULL;
  92.     }
  93.     defscr.pv_rows = Rows;
  94.     defscr.pv_cols = Columns;
  95.  
  96.     defscr.pv_window = (genptr *) xvi_startup(&defscr, argc, argv,
  97.                             getenv("XVINIT"));
  98.  
  99.     while (1) {
  100.     register int    r;
  101.  
  102.     r = inchar(timeout);
  103.     if (r == EOF) {
  104.         event.ev_type = Ev_timeout;
  105.     } else {
  106.         event.ev_type = Ev_char;
  107.         event.ev_inchar = r;
  108.     }
  109.     timeout = xvi_handle_event(&event);
  110.     }
  111. }
  112.  
  113. /*ARGSUSED*/
  114. static VirtScr *
  115. newscr(scr)
  116. VirtScr    *scr;
  117. {
  118.     return(NULL);
  119. }
  120.  
  121. /*ARGSUSED*/
  122. static void
  123. closescr(scr)
  124. VirtScr    *scr;
  125. {
  126. }
  127.  
  128. /*ARGSUSED*/
  129. static int
  130. getrows(scr)
  131. VirtScr    *scr;
  132. {
  133.     return(scr->pv_rows);
  134. }
  135.  
  136. /*ARGSUSED*/
  137. static int
  138. getcols(scr)
  139. VirtScr    *scr;
  140. {
  141.     return(scr->pv_cols);
  142. }
  143.  
  144. /*ARGSUSED*/
  145. static void
  146. clear_all(scr)
  147. VirtScr    *scr;
  148. {
  149.     erase_display();
  150. }
  151.  
  152. /*ARGSUSED*/
  153. static void
  154. clear_line(scr, row, col)
  155. VirtScr    *scr;
  156. int    row;
  157. int    col;
  158. {
  159.     tty_goto(row, col);
  160.     erase_line();
  161. }
  162.  
  163. /*ARGSUSED*/
  164. static void
  165. xygoto(scr, row, col)
  166. VirtScr    *scr;
  167. int    row;
  168. int    col;
  169. {
  170.     tty_goto(row, col);
  171. }
  172.  
  173. /*ARGSUSED*/
  174. static void
  175. xyadvise(scr, row, col, index, str)
  176. VirtScr    *scr;
  177. int    row;
  178. int    col;
  179. int    index;
  180. char    *str;
  181. {
  182.     if (index > cost_goto) {
  183.     tty_goto(row, col + index);
  184.     } else {
  185.     tty_goto(row, col);
  186.     while (--index > 0) {
  187.         outchar(*str++);
  188.     }
  189.     }
  190. }
  191.  
  192. /*ARGSUSED*/
  193. static void
  194. put_str(scr, row, col, str)
  195. VirtScr    *scr;
  196. int    row;
  197. int    col;
  198. char    *str;
  199. {
  200.     tty_goto(row, col);
  201.     outstr(str);
  202. }
  203.  
  204. /*ARGSUSED*/
  205. static void
  206. put_char(scr, row, col, c)
  207. VirtScr    *scr;
  208. int    row;
  209. int    col;
  210. int    c;
  211. {
  212.     tty_goto(row, col);
  213.     outchar(c);
  214. }
  215.  
  216. /*ARGSUSED*/
  217. static void
  218. ins_str(scr, row, col, str)
  219. VirtScr    *scr;
  220. int    row;
  221. int    col;
  222. char    *str;
  223. {
  224.     /*
  225.      * If we are called, can_inschar is TRUE,
  226.      * so we know it is safe to use inschar().
  227.      */
  228.     tty_goto(row, col);
  229.     for ( ; *str != '\0'; str++) {
  230.     inschar(*str);
  231.     }
  232. }
  233.  
  234. /*ARGSUSED*/
  235. static void
  236. pset_colour(scr, colour)
  237. VirtScr    *scr;
  238. int    colour;
  239. {
  240.     set_colour(colour);
  241. }
  242.  
  243. /*ARGSUSED*/
  244. static int
  245. colour_cost(scr)
  246. VirtScr    *scr;
  247. {
  248. #ifdef    SLINE_GLITCH
  249.     return(SLINE_GLITCH);
  250. #else
  251.     return(0);
  252. #endif
  253. }
  254.  
  255. /*ARGSUSED*/
  256. static int
  257. scroll(scr, start_row, end_row, nlines)
  258. VirtScr    *scr;
  259. int    start_row;
  260. int    end_row;
  261. int    nlines;
  262. {
  263.     if (nlines < 0) {
  264.     /*
  265.      * nlines negative means scroll reverse - i.e. move
  266.      * the text downwards with respect to the terminal.
  267.      */
  268.     nlines = -nlines;
  269.  
  270.     if (can_scroll_area) {
  271.         scroll_down(start_row, end_row, nlines);
  272.     } else if (can_ins_line && end_row == Rows - 1) {
  273.         int    line;
  274.  
  275.         for (line = 0; line < nlines; line++) {
  276.         tty_goto(start_row, 0);
  277.         insert_line();
  278.         }
  279.     } else {
  280.         return(0);
  281.     }
  282.     } else if (nlines > 0) {
  283.     /*
  284.      * Whereas nlines positive is "normal" scrolling.
  285.      */
  286.     if (can_scroll_area) {
  287.         scroll_up(start_row, end_row, nlines);
  288.     } else if (end_row == Rows - 1) {
  289.         int    line;
  290.  
  291.         if (can_del_line) {
  292.         for (line = 0; line < nlines; line++) {
  293.             tty_goto(start_row, 0);
  294.             delete_line();
  295.         }
  296.         } else if (start_row == 0) {
  297.         tty_goto(start_row, 0);
  298.         for (line = 0; line < nlines; line++) {
  299.             delete_line();
  300.         }
  301.         } else {
  302.         return(0);
  303.         }
  304.     } else {
  305.         return(0);
  306.     }
  307.     }
  308.     return(1);
  309. }
  310.  
  311. /*ARGSUSED*/
  312. static void
  313. flushout(scr)
  314. VirtScr    *scr;
  315. {
  316.     flush_output();
  317. }
  318.  
  319. /*ARGSUSED*/
  320. static void
  321. pbeep(scr)
  322. VirtScr    *scr;
  323. {
  324.     alert();
  325.     flush_output();
  326. }
  327.